package syscompat

import (
	"bufio"
	"os"
	"strings"
)

// SetHosts modifies the /etc/hosts file by updating the IP address associated with a given host.
//
// Parameters:
// - ip: The new IP address to set.
// - host: The host for which to update the IP address.
//
// Returns:
// - error: An error if there was a problem updating the /etc/hosts file, or nil if the update was successful.
func SetHosts(ip, host string) error {
	// Open the /etc/hosts file
	file, err := os.Open("/etc/hosts")
	if err != nil {
		return err
	}
	defer file.Close()

	// Read each line of the file and check if the host exists
	lines := make([]string, 0)
	scanner := bufio.NewScanner(file)
	found := false
	for scanner.Scan() {
		line := scanner.Text()
		if strings.Contains(line, host) {
			line = ip + "\t" + host
			found = true
		}
		lines = append(lines, line)
	}

	// If the host doesn't exist, add it to the lines
	if !found {
		lines = append(lines, ip+"\t"+host)
	}

	// Open the /etc/hosts file in write mode and truncate its contents
	file, err = os.OpenFile("/etc/hosts", os.O_WRONLY|os.O_TRUNC, 0644)
	if err != nil {
		return err
	}
	defer file.Close()

	// Write the updated lines to the file
	for _, line := range lines {
		_, err = file.WriteString(line + "\n")
		if err != nil {
			return err
		}
	}
	return nil
}
